package com.lf.data.search;

import com.lf.data.Cluster;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Query extends Cluster {

    public static final String GT = "$gt"; // >
    public static final String GTE = "$gte"; // >=
    public static final String LT = "$lt"; // <
    public static final String LTE = "$lte"; // <=
    public static final String EQ = "="; // 等于
    public static final String NE = "$ne"; // 不等于
    public static final String NOT = "$not"; // 非
    public static final String OR = "$or"; // 或
    public static final String IN = "$in";// in

    public Query() {
        super();
    }

    public Query(Map<String, Object> map) {
        super(map);
    }

    public Query(String name, Object value) {
        super();
        this.put(name, value);
    }

    public void idEQ(String id) {
        this.put("_id", new ObjectId(id));
    }

    /**
     * 等于
     *
     * @param name  键名
     * @param value 值
     */
    public void eq(String name, Object value) {
        this.put(name, value);
    }

    public void not(Map<String, Object> map) {
        this.put(NOT, map);
    }

    /**
     * 不等于
     *
     * @param name  键名
     * @param value 值
     */
    public void ne(String name, Object value) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(NE, value);
        this.put(name, map);
    }

    public void in(String name, List values) {
        Map<String, Object> map = new HashMap<>();
        map.put(IN, values);
        this.put(name, map);
    }

    public void idIn(List values) {
        Map<String, Object> map = new HashMap<>();
        List idlist = new ArrayList();
        for (Object obj : values) {
            idlist.add(new ObjectId(obj.toString()));
        }
        map.put(IN, idlist);
        this.put("_id", map);
    }
}
